package com.example.supplychainbackend.entity.sales;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GeneratorType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.IdGeneratorType;
import org.hibernate.annotations.ValueGenerationType;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.Generator;
import org.hibernate.id.IdentifierGenerator;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Entity
@Table(name = "contract")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ContractEntity {
    @Id
    String contractNumber;

    String contractType;
    String contractStatus;
    String approvalStatus;
    String executionStatus;
    String contractCategory;
    String contractName;
    Integer contractAmount;
    Integer deliverableQuantity;
    String customerCode;
    String customerName;
    String signDate;
    String signatory;
    String requiredDeliveryDate;
    String businessDepartment;
    String creator;
    Integer plannedQuantity;
    Integer executedQuantity;
    Integer unexecutedQuantity;
    Integer productionQuantity;
    Integer overflowQuantity;
    Integer deliveredQuantity;
    String customer;
    Integer company;
    String salesOrganization;
    String salesChannel;
    String productGroup;
    String localCurrency;
    String originalCurrency;
    String salesArea;
    String deliveryAddress;

    @ElementCollection
    @CollectionTable(
            name = "contract_attachments",
            joinColumns = @JoinColumn(name = "cid")
    )
    @Column(name = "attachment")
    List<String> attachments;

    String externalContractNumber;
    String opportunityNumber;
    String opportunityName;
    String orderType;
    String requirements;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "contract_n")
    List<DeliverableEntity> deliverable;

    String bomTemplate;

    //补充创建时间 用于溯源
    String createDate;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;


    @Transient
    private static final AtomicInteger counter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (contractNumber == null || contractNumber.equals("")) {
            contractNumber = String.format("%s%s%04d",
                    "HT", DateTimeUtils.getCurrentDateForID(), counter.getAndIncrement());
        }
        createDate = DateTimeUtils.getCurrentDateTime();
//        deliverableQuantity = deliverable.stream().mapToInt(DeliverableEntity::getQuantity).sum();
    }

    @PostUpdate
    public void afterUpdate() {
        if (deliverable != null) {
            deliverableQuantity = deliverable.stream().mapToInt(DeliverableEntity::getQuantity).sum();
        }
    }
}

